import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;


export class Subpages{
    private subpages: Map<string, Node> = new Map();
    private openedPage: Node[] = [];

    public addPage(id: string, page: Node): void {
        if (this.subpages.has(id)) {
            console.log("已存在指定ID的页面,指定ID为:" + id);
            return;
        }
        this.subpages.set(id, page);
    }

    public SetPageActive(id: string, active: boolean): void {
        const page = this.subpages.get(id);
        if (page == null) {
            console.error("不存在指定页面!指定页面ID为:" + id);
            return;
        }

        page.active = active;

        if (page.active) {
            this.openedPage.push(page);
        } else {
            const index = this.openedPage.indexOf(page);
            if (index !== -1) {
                this.openedPage.splice(index, 1);
            }
        }
    }

    public closeAllPage(): void {
        this.subpages.forEach((node) => {
            node.active = false;
        });
    }

    public solitaryPage(id: string): void {
        this.subpages.forEach((node, key) => {
            node.active = (id === key);
        });
    }

    public getPage(id: string): Node | null {
        const page = this.subpages.get(id);
        if (page == null) {
            console.error("不存在与指定ID匹配的页面!ID为:" + id);
        }
        return page;
    }

    constructor() {}
}

